package com.test.sort;

/**
 * 希尔排序
 * @author sujiafa
 * @date 2024/7/30
 */
public class ShellSort {

    public int[] sortArray(int[] nums) {

        if (nums == null) {
            return nums;
        }

        int size = nums.length;
        int gap = size / 2;
        while (gap > 0) {
            for (int i = gap; i < nums.length; i++) {
                int temp = nums[i];
                int preIndex = i - gap;
                while (preIndex >= 0 && nums[preIndex] > temp) {
                    nums[preIndex + gap] = nums[preIndex];
                    preIndex -= gap;
                }
                nums[preIndex + gap] = temp;
            }
            gap /= 2;
        }

        return nums;
    }
}
